// pages/appraise/appraise.js\
import { fetchAppraise ,fetchCommentsType } from '../../api/goods'
let Util = require('../../utils/util')
Page({
  data: {
    showComments:[],
    priTypeData:[],   //有图评论
    praiseData:[],    //好评评论
    middleData:[],    //中评
    badData:[],       //差评
    list:[],          //总数据
    status:'',       //选中状态
    goods_id:'',     //商品ID 
    index: 0,       //全部评论截取下标
    typeIndex:0 ,    //有图评论截取下标
    praiseIndex:0 ,  //好评评论截取下标
    middleIndex:0,
    badIndex:0,
  },
  // 时间转换函数
  timeSwitch(value){
    value.map(item => {
      item.comment_time = Util.formatTime(item.comment_time)
      return item
    })
    return value
  },
  //  生命周期函数--监听页面加载
  onLoad: function (options) {
    let { id } = options
    this.setData({
      goods_id:id
    })
    // 获取路由传过来的参数 （商品的ID：goods_id）
    this.fetchAppraiseContent(id)
  },
  // 全部
  async fetchAppraiseContent(id) {
    let { data } = await fetchAppraise(id);
    this.setData({
      ...data
    })
    this.loadMore()
  },
  // 加载更多
  loadMore(){
    const index = this.data.index
    let newComment = this.data.list.slice(index * 10, index * 10 + 10)
    newComment = this.timeSwitch(newComment)
    this.setData({
      showComments: this.data.showComments.concat(newComment),
      index: this.data.index + 1
    })
  },
  // 全部加载更多
  allCommentType(e){
    const goods_id = this.data.goods_id
    const type = e.target.dataset.type
    if(type === undefined){
      this.setData({
        status:''
      })
      this.fetchAppraiseContent(goods_id)
    }
  },
  // 有图评论类型
  async prictureType(e) {
    const goods_id = this.data.goods_id
    const type = e.target.dataset.type
    let result = await fetchCommentsType(goods_id, type)
    if(result.data.length === 0){
        wx.showToast({
          title: '没有评论',
        })
        return
    }
    this.setData({
      list:result.data,
      status:type,
    })
    this.loadMoreTypeData()
  },
  loadMoreTypeData(){
    let currentIndex = this.data.typeIndex
    let priTypeData = this.data.list.slice(currentIndex * 10,currentIndex * 10 + 10)
    if(priTypeData.length === 0){
      wx.showToast({
        title: '评论到底了',
      })
      return
    }
    // 时间转换
    priTypeData = this.timeSwitch(priTypeData)
    this.setData({
      priTypeData:this.data.priTypeData.concat(priTypeData),
      typeIndex: this.data.typeIndex + 1
    })
  },
  // 好评
  async praiseType(e){
    const goods_id = this.data.goods_id
    const type = e.target.dataset.type
    let result = await fetchCommentsType(goods_id, type)
    if(result === 0){
      wx.showToast({
        title: '没有评论',
      })
      return
    }
    this.setData({
      list:result.data,
      status:type
    })
    this.loadMorePraise()
  },
  loadMorePraise(){
    let praiseIndex = this.data.praiseIndex
    let praiseData = this.data.list.slice(praiseIndex * 10,praiseIndex * 10 + 10)
    if(praiseData === 0){
      wx.showToast({
        title: '评论到底了',
      })
      return
    }
    // 时间转换
    praiseData = this.timeSwitch(praiseData)
    this.setData({
      praiseData:this.data.praiseData.concat(praiseData),
      praiseIndex: this.data.praiseIndex + 1
    })
  },
  // 中评
  async middleType(e){
    const goods_id = this.data.goods_id
    const type = e.target.dataset.type
    let result = await fetchCommentsType(goods_id, type)
    if(result.data.length === 0){
      wx.showToast({
        title: '没有评论',
      })
      return
    }
    this.setData({
      list:result.data,
      status:type,
    })
    this.loadMoreMiddle()
  },
  loadMoreMiddle(){
    let middleIndex = this.data.middleIndex
    let middleData = this.data.list.slice(middleIndex * 10,middleIndex * 10 + 10)
    if(middleData.length === 0){
      wx.showToast({
        title: '评论到底了',
      })
      return
    }
    // 时间转换
    middleData = this.timeSwitch(middleData)
    this.setData({
      middleData:this.data.middleData.concat(middleData),
      middleIndex: this.data.middleIndex + 1
    })
  },
  // 差评
  async badType(e){
    const goods_id = this.data.goods_id
    const type = e.target.dataset.type
    let result = await fetchCommentsType(goods_id, type)
    if(result.data.length === 0){
      wx.showToast({
        title: '没有评论',
      })
      return
     }
    this.setData({
      list:result.data,
      status:type
    })
    this.loadMorebad()
  },
  loadMorebad(){
    let badIndex = this.data.badIndex
    let badData = this.data.list.slice(badIndex * 10,badIndex * 10 + 10)
    if(badData.length === 0){
      wx.showToast({
        title: '评论到底了',
      })
      return
    }
    // 时间转换
    badData = this.timeSwitch(badData)
    this.setData({
      badData:this.data.badData.concat(badData),
      badIndex: this.data.badIndex + 1
    })
  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function () {
    switch (this.data.status){
      case '':
          this.loadMore()
        break;
      case '0':
          this.loadMoreTypeData()
        break;
      case '1':
          this.loadMorePraise()
        break;
      case '2':
        this.loadMoreMiddle()
      break;
      case '3':
        this.loadMorebad()
      break;
    }
  },
})